package utils.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.ClassifoImgResult;
import tools.ItemOnClickListener;
import tools.ItemOnClickListenerOther;
import utils.viewtools.ViewPagerScrollerMaster;
import volley.VolleySingleton;

/**
 * Created by dllo on 15/11/23.
 */
public class ClassInfoImgRecycerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ClassifoImgResult.CategoryContentsEntity.ListEntity> listEntity;
    private static final int IS_VIEWPAGE = 0;
    private static final int IS_RECYCLERVIEW = 1;
    private static final int IS_FOOTER = 2;

    private ItemOnClickListenerOther itemOnClickListenerOther;
    private ItemOnClickListener itemOnClickListener;
    private FragmentManager manager;
    private List<ClassifoImgResult.FocusImagesEntity.ListEntity> list;
    private List<ClassifoImgResult.TagsEntity.ListEntity> tagListEntity;
    private Context mContext;

     public void setItemOnClick(ItemOnClickListener itemOnClickListener){
         this.itemOnClickListener = itemOnClickListener;
     }
    public void setItemListen (ItemOnClickListenerOther itemOnClickListenerOther){
        this.itemOnClickListenerOther = itemOnClickListenerOther;

    }

    public ClassInfoImgRecycerAdapter(FragmentManager manager) {
        this.manager = manager;
    }

    public void addData(List<ClassifoImgResult.CategoryContentsEntity.ListEntity> datas) {
        listEntity = datas;
        notifyDataSetChanged();
    }

    public void addFootItemData(List<ClassifoImgResult.TagsEntity.ListEntity> datas) {
        this.tagListEntity = datas;
        notifyDataSetChanged();
    }

    public void addViewPagerData(List<ClassifoImgResult.FocusImagesEntity.ListEntity> list) {
        this.list = list;
        notifyDataSetChanged();

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        mContext = parent.getContext();
        switch (viewType) {
            case IS_VIEWPAGE:
                return new ViewHolder00(inflater.inflate(R.layout.classifoimg_viewpage, null));
            case IS_RECYCLERVIEW:
                return new ViewHolder01(inflater.inflate(R.layout.classifoimg_classificationpate_item_recyclerview, null));
            case IS_FOOTER:
                return new ViewHolder02(inflater.inflate(R.layout.classifoimg_footer, null));
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder00) {
            ViewpagerAdapter adapter = new ViewpagerAdapter(manager);
            ((ViewHolder00) holder).viewPager.setAdapter(adapter);
            adapter.addData(list);
            ((ViewHolder00) holder).classIfoImg_viewpage_title.setText(listEntity.get(0).getList().get(0).getTitle());
            ((ViewHolder00) holder).ClassIfoImg_viewpage_subtitle.setText(listEntity.get(0).getList().get(0).getSubtitle());

        }

        if (holder instanceof ViewHolder01) {
            if (listEntity != null) {
                ((ViewHolder01) holder).classIfoImg_toastTitle.setText(listEntity.get(position).getTitle());
                List<ClassifoImgResult.CategoryContentsEntity.ListEntity.ListTEntity> listTEntity =
                        listEntity.get(position).getList();
                if (listTEntity != null) {
                    if (listEntity.size() > 0) {
                        ((ViewHolder01) holder).classIfoImg_firstTitle.setText(listTEntity.get(0).getTitle());
                        ((ViewHolder01) holder).classIfoImg_firstMinTitle.setText(listTEntity.get(0).getIntro());
                        VolleySingleton.getInstance().getBitmap(listTEntity.get(0).getAlbumCoverUrl290(), ((ViewHolder01) holder).classIfoImg_firstImg);
                        double firstPlaysCounts = ((double) listTEntity.get(0).getPlaysCounts() / 10000);
                        String firstPC = String.format("%1$.2f", firstPlaysCounts);
                        ((ViewHolder01) holder).classIfoImg_firstPlaysCounts.setText(firstPC + "万");
                        ((ViewHolder01) holder).classIfoImg_firstTracksCounts.setText(listTEntity.get(0).getTracksCounts() + "集");

                        if (listTEntity.size() > 1) {
                            ((ViewHolder01) holder).classIfoImg_secondTitle.setText(listTEntity.get(1).getTitle());
                            ((ViewHolder01) holder).classIfoImg_secondMinTitle.setText(listTEntity.get(1).getIntro());
                            VolleySingleton.getInstance().getBitmap(listTEntity.get(1).getAlbumCoverUrl290(), ((ViewHolder01) holder).classIfoImg_secondImg);
                            double secondPlaysCounts = ((double) listTEntity.get(1).getPlaysCounts() / 10000);
                            String secondPC = String.format("%1$.2f", secondPlaysCounts);
                            ((ViewHolder01) holder).classIfoImg_firstPlaysCounts.setText(secondPC + "万");
                            ((ViewHolder01) holder).classIfoImg_secondTracksCounts.setText(listTEntity.get(1).getTracksCounts() + "集");

                            if (listTEntity.size() > 2) {
                                ((ViewHolder01) holder).classIfoImg_thirdTitle.setText(listTEntity.get(2).getTitle());
                                ((ViewHolder01) holder).classIfoImg_thirdMinTitle.setText(listTEntity.get(2).getIntro());
                                VolleySingleton.getInstance().getBitmap(listTEntity.get(2).getAlbumCoverUrl290(), ((ViewHolder01) holder).classIfoImg_thirdImg);
                                double thirdPlaysCounts = ((double) listTEntity.get(2).getPlaysCounts() / 10000);
                                String thirdPC = String.format("%1$.2f", thirdPlaysCounts);
                                ((ViewHolder01) holder).classIfoImg_firstPlaysCounts.setText(thirdPC + "万");
                                ((ViewHolder01) holder).classIfoImg_thirdTracksCounts.setText(listTEntity.get(2).getTracksCounts() + "集");
                            } else {
                                ((ViewHolder01) holder).thirdLinear.setVisibility(View.GONE);
                            }
                        } else {
                            ((ViewHolder01) holder).secondLinear.setVisibility(View.GONE);
                            ((ViewHolder01) holder).thirdLinear.setVisibility(View.GONE);
                        }
                        ((ViewHolder01) holder).itemposition = position;
                    }
                }
            }

        }

        if (holder instanceof ViewHolder02) {

            ((ViewHolder02) holder).classViewHolder02Adapter.addFootItemData(tagListEntity);
        }
    }


    public class ViewHolder00 extends RecyclerView.ViewHolder {
        private ViewPager viewPager;
        private ViewpagerAdapter viewpagerAdapter;
        private ViewPagerScrollerMaster viewPagerScrollerMaster;
        private TextView classIfoImg_viewpage_title, ClassIfoImg_viewpage_subtitle;


        public ViewHolder00(View itemView) {
            super(itemView);
            viewPager = (ViewPager) itemView.findViewById(R.id.classIfoImg_viewPage);
            viewPagerScrollerMaster = new ViewPagerScrollerMaster();
            viewPagerScrollerMaster.setRunnable(viewPager);
            viewPagerScrollerMaster.startTurning(8000);

            classIfoImg_viewpage_title = (TextView) itemView.findViewById(R.id.classIfoImg_viewpage_title);
            ClassIfoImg_viewpage_subtitle = (TextView) itemView.findViewById(R.id.classIfoImg_viewpage_subtitle);


        }


    }

    public class ViewHolder01 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView classIfoImg_toastTitle, classIfoImg_firstTitle, classIfoImg_secondTitle,
                classIfoImg_thirdTitle, classIfoImg_firstMinTitle, classIfoImg_secondMinTitle,
                classIfoImg_thirdMinTitle, classIfoImg_firstPlaysCounts, classIfoImg_secondPlaysCounts,
                classIfoImg_thirdPlaysCounts, classIfoImg_firstTracksCounts, classIfoImg_secondTracksCounts,
                classIfoImg_thirdTracksCounts;
        private ImageView classIfoImg_firstIforUnfold, classIfoImg_secondIforUnfold, classIfoImg_thirdIforUnfold,
                classIfoImg_toastUnfold;
        private NetworkImageView classIfoImg_firstImg, classIfoImg_secondImg, classIfoImg_thirdImg;
        private LinearLayout allLinear, firstLinear, secondLinear, thirdLinear;
        private int itemposition;

        public ViewHolder01(View itemView) {
            super(itemView);
            classIfoImg_toastTitle = (TextView) itemView.findViewById(R.id.classIfoImg_ToastTitle);
            classIfoImg_firstTitle = (TextView) itemView.findViewById(R.id.classIfoImg_firstTitle);
            classIfoImg_secondTitle = (TextView) itemView.findViewById(R.id.classIfoImg_secondTitle);
            classIfoImg_thirdTitle = (TextView) itemView.findViewById(R.id.classIfoImg_thirdTitle);
            classIfoImg_firstMinTitle = (TextView) itemView.findViewById(R.id.classIfoImg_firstMinTitle);
            classIfoImg_secondMinTitle = (TextView) itemView.findViewById(R.id.classIfoImg_secondMinTitle);
            classIfoImg_thirdMinTitle = (TextView) itemView.findViewById(R.id.classIfoImg_thirdMinTitle);
            classIfoImg_firstPlaysCounts = (TextView) itemView.findViewById(R.id.classIfoImg_firstPlaysCounts);
            classIfoImg_secondPlaysCounts = (TextView) itemView.findViewById(R.id.classIfoImg_secondPlaysCounts);
            classIfoImg_thirdPlaysCounts = (TextView) itemView.findViewById(R.id.classIfoImg_thirdPlaysCounts);
            classIfoImg_firstTracksCounts = (TextView) itemView.findViewById(R.id.classIfoImg_firstTracksCounts);
            classIfoImg_secondTracksCounts = (TextView) itemView.findViewById(R.id.classIfoImg_secondTracksCounts);
            classIfoImg_thirdTracksCounts = (TextView) itemView.findViewById(R.id.classIfoImg_thirdTracksCounts);

            classIfoImg_firstIforUnfold = (ImageView) itemView.findViewById(R.id.classIfoImg_firstInforUnfold);
            classIfoImg_secondIforUnfold = (ImageView) itemView.findViewById(R.id.classIfoImg_secondInforUnfold);
            classIfoImg_thirdIforUnfold = (ImageView) itemView.findViewById(R.id.classIfoImg_thirdInforUnfold);
            classIfoImg_firstImg = (NetworkImageView) itemView.findViewById(R.id.classIfoImg_firstImg);
            classIfoImg_secondImg = (NetworkImageView) itemView.findViewById(R.id.classIfoImg_secondImg);
            classIfoImg_thirdImg = (NetworkImageView) itemView.findViewById(R.id.classIfoImg_thirdImg);

            allLinear = (LinearLayout) itemView.findViewById(R.id.allLinear);
            firstLinear = (LinearLayout) itemView.findViewById(R.id.firstLinear);
            secondLinear = (LinearLayout) itemView.findViewById(R.id.secondLinear);
            thirdLinear = (LinearLayout) itemView.findViewById(R.id.thirdLinear);

            firstLinear.setOnClickListener(this);
            secondLinear.setOnClickListener(this);
            thirdLinear.setOnClickListener(this);

        }

//01的监听
        @Override
        public void onClick(View v) {
            if (listEntity!= null){
                List<ClassifoImgResult.CategoryContentsEntity.ListEntity.ListTEntity> li =
                        listEntity.get(itemposition).getList();
            switch (v.getId()){
                case R.id.firstLinear:
                    itemOnClickListener.itemListener(li.get(0));
                            ;
                    break;
                case R.id.secondLinear:
                    itemOnClickListener.itemListener(
                            li.get(1));
                    break;
                case R.id.thirdLinear:
                    itemOnClickListener.itemListener(
                            li.get(2));
                    break;
            }
        }
        }
    }

    public class ViewHolder02 extends RecyclerView.ViewHolder {
        private RecyclerView classIfoImg_ViewHlder02_recyclerView;
        private ClassViewHolder02Adapter classViewHolder02Adapter;

        public ViewHolder02(View itemView) {
            super(itemView);
            classIfoImg_ViewHlder02_recyclerView = (RecyclerView) itemView.findViewById(
                    R.id.classIfoImg_ViewHlder02_recyclerView);
            GridLayoutManager gm = new GridLayoutManager(mContext, 3, LinearLayoutManager.VERTICAL, false);

            classIfoImg_ViewHlder02_recyclerView.setLayoutManager(gm);
            classViewHolder02Adapter = new ClassViewHolder02Adapter();
            classIfoImg_ViewHlder02_recyclerView.setAdapter(classViewHolder02Adapter);

        }
    }

    public boolean isLength() {
        return listEntity != null && listEntity.size() > 0;
    }

    @Override
    public int getItemCount() {
        return isLength() ? (listEntity.size() + 1) : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return IS_VIEWPAGE;
        } else if (position + 1 < listEntity.size() + 1) {
            return IS_RECYCLERVIEW;
        } else
            return IS_FOOTER;
    }
}
